<?php

namespace app\v1\model;

use think\Model;
use think\Db;

/**
 * Class Message
 * @package app\v1\model
 * 模块说明：这个类是用来处理短信验证码的发送
 */
class Message extends Model
{
    //发送短信验证码
    public function sendSm($phone){
        $redis=getRedis();
        $codeInfo=$redis->get('verify'.$phone);
        if(!empty($codeInfo)){
            if(time()-$codeInfo['sendTime']<60){
                renderInfo(400001,'短信发送太频繁,请稍后重试');
            }
        }
        $this->checkPhone($phone);
        $verifyCode=$this->getVerifyCode();
        $data=array(
            'account'=>config('account'),
            'pswd'=>config('password'),
            'mobile'=>$phone,
            'msg'=>$verifyCode.'（微信验证码）尊敬的好托管用户，请勿将此验证码泄露给他人！'
        );
        $info=httpRequest(config('message_url'),$data);
        $infoArray=explode(',',$info);
        $status=(int)$infoArray[1];
        if($status==0){
            $codeInfo=array();
            $codeInfo['verifyCode']=$verifyCode;
            $codeInfo['sendTime']=time();
            $redis->set(config('verify').$phone,$codeInfo);
            renderInfo(0,'验证发送成功');
        }else{
            renderInfo(400008,'系统繁忙,请稍后重试');
        }



    }
    //验证手机号码是否属于平台
    public function checkPhone($phone){
        $map['phone']=$phone;
        $res=Db::name('Users')->where($map)->find();
        if(empty($res)){
            renderInfo(400001,'该手机号还未注册平台账号');
        }
    }
    //获取随机六位数验证码
    public function getVerifyCode(){
        return rand(100000,999999);
    }
}
